from django.db import models
from django.forms import forms
from django.forms.fields import ChoiceField
from django.forms.models import ModelForm
import sys

class Option(models.Model):
    """ This is a dummy class for use with the admin interface """
    pass

class OptionType(models.Model):
    """
    This is a base class for all the types of options.
    
    Members:
        app - the name of the app this option applies to
        name - the name of this option
        default - the default value of the option
        description - a textual description of what the option does
    """
    app = models.CharField(max_length=255, editable=False)
    name = models.CharField(max_length=255, editable=False)
    default = models.CharField(max_length=255, editable=False)
    description = models.CharField(max_length=255, editable=False)
    def get_name(self):
        return self.name
    def save(self, *args, **kwargs):
        #sys.stderr.write("Saving option %s\n" % self.name); sys.stderr.flush();
        super(OptionType,self).save(*args, **kwargs)
    class Meta:
        abstract = True
class OptionTypeForm(ModelForm):
    class Meta:
        model = OptionType
        exclude = ['app','name']

class DisplayNameOption(models.Model):
    app = models.CharField(max_length=255, editable=False)
    value = models.CharField(max_length=255)

class EnableOption(models.Model):
    app = models.CharField(max_length=255, editable=False)
    default = models.CharField(max_length=255, editable=False)
    description = models.CharField(max_length=255, editable=False)
    value = models.BooleanField(default=False)
    def name(self):
        return "_enable"
class EnableOptionForm(ModelForm):
    class Meta:
        model = EnableOption
        exclude = ['app', 'default', 'description']
        
class BooleanOption(OptionType):
    value = models.BooleanField(default=False)
class BooleanOptionForm(OptionTypeForm):
    class Meta:
        model = BooleanOption
        
class ChoiceOption(OptionType):
    value = models.CharField(max_length=255)
    choices = models.TextField(editable=False)
        
    def get_choices(self):
        return eval(self.choices)
        
class ChoiceOptionForm(OptionTypeForm):
    value = ChoiceField()
    def __init__(self, *args, **kwargs):
        super(ChoiceOptionForm,self).__init__(*args, **kwargs)
        self.fields['value'].choices = self.instance.get_choices()
    class Meta:
        model = ChoiceOption

class IntOption(OptionType):
    value = models.IntegerField()
class IntOptionForm(OptionTypeForm):
    class Meta:
        model = IntOption
        
class FloatOption(OptionType):
    value = models.FloatField()
class FloatOptionForm(OptionTypeForm):
    class Meta:
        model = FloatOption
        
class TextOption(OptionType):
    value = models.CharField(max_length=255)
class TextOptionForm(OptionTypeForm):
    class Meta:
        model = TextOption
        widgets = {
            'value': forms.TextInput(attrs={'size': 60}),
        }